﻿using Microsoft.Extensions.Configuration;

namespace ThresholdAssistant.ConfigSdk
{
    /// <summary>
    /// 配置文件扩展
    /// </summary>
    public static class AppSettingExtensions
    {
        /// <summary>
        /// 获取配置文件的VALUE
        /// </summary>
        /// <param name="configuration">配置文件</param>
        /// <param name="key">配置文件的Key(格式：xxx:yyy，注意中间使用':'分割)</param>
        /// <returns></returns>
        public static string GetValue(this IConfiguration configuration, string key)
        {
            return configuration.GetValue<string>(key);
        }

        /// <summary>
        /// 获取配置文件的VALUE
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="configuration">配置文件</param>
        /// <param name="key">配置文件的Key(格式：xxx:yyy，注意中间使用':'分割)</param>
        /// <returns></returns>
        public static T GetValue<T>(this IConfiguration configuration, string key)
        {
            T value = default;
            if (configuration == null)
            {
                return value;
            }
            return configuration.GetSection(key).Get<T>();
        }

        /// <summary>
        /// 获取Json配置
        /// </summary>
        /// <param name="configuration">配置文件</param>
        /// <returns></returns>
        public static T GetSettings<T>(this IConfiguration configuration)
        {
            T value = default;
            if (configuration == null)
            {
                return value;
            }
            return ConfigurationBinder.Get<T>(configuration);
        }
    }
}